import Vue from 'vue'
import App from './App'
import router from './router'
import axios from 'axios'
import store from './vuex/index'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
//导入fontawesome
import { library } from '@fortawesome/fontawesome-svg-core';
import { FontAwesomeIcon } from '@fortawesome/vue-fontawesome';
//引入图标
import {
  faListAlt,//查看信息图标
  faEdit,//编辑图标
  faTrashAlt,//删除图标
  faFileAlt,//查看信息图标
  faPaperPlane,//推送
} from '@fortawesome/free-regular-svg-icons';
import {
  faSyncAlt,
  faExpandArrowsAlt,
}from '@fortawesome/free-solid-svg-icons';
library.add(
  faListAlt,
  faEdit,
  faTrashAlt,
  faSyncAlt,
  faExpandArrowsAlt,
  faFileAlt,
  faPaperPlane
);
//引入全屏
import fullscreen from 'vue-fullscreen'

//注册全屏组件
Vue.use(fullscreen)

//注册Font Awesome组件
Vue.component('font-awesome-icon', FontAwesomeIcon);

//路由拦截器
router.beforeEach((to, from, next) => {
  if(to.meta.notRequestAuth){
    next();
  }else{
    if(window.sessionStorage.getItem("token")) { // 通过vuex state获取当前的token是否存在
      next();
    }else {
      next({
      path: '/',
       query: {redirect: to.fullPath} // 将跳转的路由path作为参数，登录成功后跳转到该路由
      })
    }
  }
  // if (to.meta.requireAuth) { // 判断该路由是否需要登录权限
  //   if(window.sessionStorage.getItem("token")) { // 通过vuex state获取当前的token是否存在
  //     next();
  //   }else {
  //     next({
  //     path: '/',
  //      query: {redirect: to.fullPath} // 将跳转的路由path作为参数，登录成功后跳转到该路由
  //     })
  //   }
  // }
  // else {
  //  next();
  // }
 })

Vue.use(ElementUI)
Vue.config.productionTip = false

let http = axios.create();

//请求拦截器
http.interceptors.request.use(
  config => {
    //从sessionstorage中获取token、refreshtoken携带上
    let token = window.sessionStorage.getItem("token");
    let refreshtoken = window.sessionStorage.getItem("refreshtoken");
    if(token){
      config.headers.authorization = token;
    }
    if(refreshtoken){
      config.headers.refreshtoken = refreshtoken;
    }
    return config;
  }
);
//响应拦截
http.interceptors.response.use(
  response =>{
    //获取token
    let token = response.headers.authorization;
    let refreshtoken = response.headers.refreshtoken;
    //
    if(token){
      window.sessionStorage.setItem("token",token);
    }
    if(refreshtoken){
      window.sessionStorage.setItem("refreshtoken",refreshtoken);
    }
    //
    return response;
  }
);


Vue.prototype.axios = http;
Vue.prototype.baseUrl = "http://192.168.8.161:8002/"
//Vue.prototype.baseUrl = "http://localhost:8002/"
Vue.prototype.$store = store;


/* eslint-disable no-new */
new Vue({
  el: '#app',

  router,
  components: { App },
  template: '<App/>'
})
